# -*- coding: UTF-8 -*-

import xeres

__all__ = [ 'ComponentDef' , 'ContainerDef' ]

#
#	Defines components in uxml
#

# Component definition.
class ComponentDef(object):
	def __init__( self, typename, template, entry ):
		# Template is class of element, engine will create element by this template.
		# e.g.  'xeres.XButton', engine will import xeres and instance an XForm class as element.
		self.template = template
		self.typename = typename
		
		# Name is used in codes, while runtime as a global variable for controlling the element.
		# e.g. id="button1" makes you can access this element in script by button1 variable.
		# Also name is used in style sheet. You can specify a style for specific named components.
		self.id = None
		
		# Style specifies which style will be used in this component.
		# If no style is assigned, default style of this class type will be use, while if name
		# is assigned, overrided style or style attribute will be apply.
		self.style = {}
		self.stylename = None
		
		# Property map stores all assigned properties.
		self.properties = {}
		
		for name,attr in entry.attributes:
			if name == "id":
				self.id = attr
			elif name == "class":
				self.stylename = attr
			else:
				self.properties[name] = attr
	
	# if it's a container
	def is_container(self):
		return False
	
	def list_children(self):
		return []
		
	def get_property(self,prop):
		if prop == "id":
			return self.id
		elif prop == "class":
			return self.stylename
		return self.properties.get(prop)

# Container definition.
class ContainerDef(ComponentDef):
	def __init__( self, typename, template, entry, children ):
		super(ContainerDef,self).__init__( typename, template, entry )
		# Children in container.
		self.children = children
		assert issubclass( template , xeres.IGuiForm )

	# if it's a container
	def is_container(self):
		return True
	
	def list_children(self):
		return self.children[0:]
